package com.hxzy.projectssmplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.projectssmplus.dto.SysAdminUserDTO;
import com.hxzy.projectssmplus.entity.SysAdminUser;
import com.hxzy.projectssmplus.service.SysAdminUserService;
import com.hxzy.projectssmplus.vo.AjaxResult;
import com.hxzy.projectssmplus.vo.AjaxTableInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class SysAdminUserController extends BaseController{

    @Autowired
    private SysAdminUserService sysAdminUserService;

    @GetMapping(value = "/user/search")
    public AjaxTableInfo ajaxData(SysAdminUserDTO sysAdminUserDTO){
        IPage<SysAdminUser> page = this.sysAdminUserService.search(super.startPage(), sysAdminUserDTO);
        return super.getTableInfo(page);
    }

    @PostMapping(value = "/user/add")
    public AjaxResult add(@Validated @RequestBody SysAdminUser sysAdminUser){
        boolean save = this.sysAdminUserService.save(sysAdminUser);
        if (save) {
            return AjaxResult.success("新增成功");
        }else {
            return AjaxResult.success("新增失败");
        }

    }

    @PostMapping (value = "/user/edit")
    public AjaxResult update(@Validated @RequestBody SysAdminUser sysAdminUser){
        boolean updateById = this.sysAdminUserService.updateById(sysAdminUser);
        if (updateById) {
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.success("修改失败");
        }
    }


    @PostMapping(value = "/user/{id}/delete")
    public AjaxResult del(@RequestBody @PathVariable(value = "id")  Integer id){
        boolean removeById = this.sysAdminUserService.removeById(id);

        if (removeById) {
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.success("删除失败");
        }
    }
}
